package com.haikesi.util;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

/**
 * @ClassName LogApiUtils
 * @Author HandsomeBugs
 * @Date 2019/5/27 14:28
 * @Version 1.0
 **/
@Component
public class LogApiUtils {

    private static RestTemplate restTemplate;

    @Autowired
    private  LogApiUtils( RestTemplate restTemplate){
        LogApiUtils.restTemplate=restTemplate;
    }


    private static Log log = LogFactory.getLog(LogApiUtils.class);
    private static String serviceUrl = "http://SERVICE-LOG";



    /**
     * 添加系统日志
     * @param controlName 操作人名
     * @param logComment 操作内容
     * @param logType 操作类型
     * @param userId 用户ID
     */
    public static void add_Sys_Log(String controlName,String logComment,String logType,String userId){
        String url=serviceUrl+"/api/sysLog";
        log.info("===========添加系统日志:" + url);
        JSONObject requestBody = new JSONObject();
        requestBody.put("controlName", controlName);
        requestBody.put("logComment", logComment);
        requestBody.put("logType", logType);
        requestBody.put("userId", userId);
        log.info("===========请求URl:" + url);
        log.info("===========请求参数:" + requestBody.toString());
        try {
            ResponseEntity<JSONObject> response = restTemplate.postForEntity(url, requestBody,  JSONObject.class);
            log.info("===========ResponseEntity:" + response);
        }catch (Exception e){
            log.info("===========请求失败:"+e.getMessage() );
            return;
        }
    }


    /**
     * 添加登陆日志
     * @param logIp 登陆IP
     * @param loginRegion 登陆地区
     * @param loginName 登陆账号
     * @param userId 用户ID
     */
    public static void add_Login_Log(String logIp,String loginRegion,String loginName,String userId){
        String url=serviceUrl+"/api/loginLog";
        log.info("===========添加登陆日志:" + url);
        JSONObject requestBody = new JSONObject();
        requestBody.put("logIp", logIp);
        requestBody.put("loginRegion", loginRegion);
        requestBody.put("loginName", loginName);
        requestBody.put("userId", userId);
        log.info("===========请求URl:" + url);
        log.info("===========请求参数:" + requestBody.toString());
        try {
            ResponseEntity<JSONObject> response = restTemplate.postForEntity(url, requestBody,  JSONObject.class);
            log.info("===========ResponseEntity:" + response);
        }catch (Exception e){
            log.info("===========请求失败:"+e.getMessage() );
            return;
        }
    }

    /**
     * 添加操作日志
     * @param controlName 操作人名
     * @param controlComment 操作内容
     * @param controlType 操作类型
     * @param userId 用户ID
     */
    public static void add_Control_Log(String controlName,String controlComment,String controlType,String userId){
        String url=serviceUrl+"/api/controlLog";
        log.info("===========添加系统日志:" + url);
        JSONObject requestBody = new JSONObject();
        requestBody.put("controlName", controlName);
        requestBody.put("controlComment", controlComment);
        requestBody.put("controlType", controlType);
        requestBody.put("userId", userId);
        log.info("===========请求URl:" + url);
        log.info("===========请求参数:" + requestBody.toString());
        try {
            ResponseEntity<JSONObject> response = restTemplate.postForEntity(url, requestBody,  JSONObject.class);
            log.info("===========ResponseEntity:" + response);
        }catch (Exception e){
            log.info("===========请求失败:"+e.getMessage() );
            return;
        }
    }
}